package com.dao.Impl;

import com.dao.InfoDao;
import com.pojo.Info;
import com.util.JDBC;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class InfoDaoImpl implements InfoDao {
    @Override
    public List<Info> queryInfo() {
        String sql = "SELECT * FROM keyword WHERE id > ? ORDER BY id DESC";
        Object[] objects = new Object[1];
        objects[0] = 0;
        ResultSet resultSet = JDBC.query(sql,objects);
        List<Info> infoList = new ArrayList<Info>();
        try{
            while (resultSet.next()){
                Info info = new Info();
                info.setId(resultSet.getInt(1));
                info.setName(resultSet.getString(2));
                info.setType(resultSet.getString(3));
                info.setDate(resultSet.getString(4));
                infoList.add(info);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return infoList;
    }

    @Override
    public int delInfo(Info info) {
        String sql = "DELETE FROM keyword WHERE id = ?";
        Object[] objects = new Object[1];
        objects[0] = info.getId();
        int num = 0;
        num = JDBC.alter(sql,objects);
        return num;
    }

    @Override
    public int addInfo(Info info) {
        String sql = "INSERT INTO information.keyword (name, type, createDate) VALUE(?,?,?)";
        Object[] objects = new Object[3];
        objects[0] = info.getName();
        objects[1] = info.getType();
        objects[2] = info.getDate();
        int num = 0;
        num = JDBC.alter(sql,objects);
        return num;
    }

    @Override
    public List<Info> queryInfo(Info info1) {
        String sql = "SELECT * FROM keyword WHERE name = ?";
        Object[] objects = new Object[1];
        objects[0] = info1.getName();
        ResultSet resultSet = JDBC.query(sql,objects);
        List<Info> infoList = new ArrayList<Info>();
        try{
            while (resultSet.next()){
                Info info = new Info();
                info.setId(resultSet.getInt(1));
                info.setName(resultSet.getString(2));
                info.setType(resultSet.getString(3));
                info.setDate(resultSet.getString(4));
                infoList.add(info);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return infoList;
    }
}
